home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9095 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  75 lines

  1. Path: newsfeed.direct.ca!usenet
  2. From: qjackson@direct.ca
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: What does this "const" mean?
  5. Date: Wed, 28 Feb 1996 16:51:17 GMT
  6. Organization: Parsepolis Software
  7. Message-ID: <4h218d$2ar@aphex.direct.ca>
  8. References: <4h0j7u$htp@crcnis3.unl.edu>
  9. Reply-To: qjackson@direct.ca
  10. NNTP-Posting-Host: 204.174.249.137
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. chijang@cse.unl.edu (Chi-Jang Huang) wrote:
  14.  
  15. >         class intset {
  16. >             // ...
  17. >             void start(int& i) const   { i = 0; }
  18. >             int ok(int& i) const       { return i<cursize; }
  19. >             int next(int& i) const     { return x[i++]; }
  20. >         };
  21.  
  22. >     What does "const" mean in this program segment?  What role does
  23. >it play?  In what kind of situation will we use "const" like this?
  24. >Any comments or suggestions are highly appreciated.  Thanks.
  25.  
  26. When a member function is declared const, it is flagged by the
  27. compiler as being known not to change any member data of the class.
  28. Only const member functions can be invoked with const objects.  For
  29. instance:
  30.  
  31.     class foo
  32.     {
  33.         public:
  34.         int quux;
  35.  
  36.         // bar does not change quux
  37.         void bar (void) const { /* ... */ }
  38.  
  39.         // baz can change quux, if it has to
  40.         void baz (void)       {/* .... */}
  41.  
  42.     };
  43.  
  44.     const foo someFoo;
  45.  
  46.     someFoo.bar(); // LEGAL becase someFoo and bar are both const
  47.  
  48.     someFoo.baz(); // ILLEGAL because someFoo is const, 
  49.                // and baz isn't
  50.  
  51. From what I've seen of C++ code, const member functions are used
  52. primarily as member functions whose main purpose is to return the
  53. value of member data members, or the value of calculations made on
  54. member data members.  They can also be used to change the value of one
  55. or more of their parameters according to some internal logic of the
  56. class, as in the case of intset::next(int& i) in your snippet.
  57.  
  58. The only "suggestion" I can make is to declare any member function
  59. that does not alter any of its class's member data members as const,
  60. since this will free up as many of the member functions for use by
  61. objects declared as const as possible.
  62.  
  63.  
  64. Hope that helps....
  65.  
  66.  
  67.  
  68. --                           
  69.                            | 
  70.     Parsepolis Software    |   Quinn Tyler Jackson
  71.         "ParseCity"        |     (aka 'Jamshid')
  72. >--------------------------|   qjackson@direct.ca
  73.                            |---------------------->
  74.  
  75.